﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using Library.UI.Grid;
using NCKH.Controllers;
using NCKH.models;
using NCKH.View.ThongKe;


namespace NCKH.ThongKe
{
    public partial class frmTKLopTinChi : DevExpress.XtraEditors.XtraForm
    {
        public static string MaHocPhan { get; set; }
        public frmTKLopTinChi()
        {
            InitializeComponent();
        }

        private void labelControl8_Click(object sender, EventArgs e)
        {

        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void frmTKLopTinChi_Load(object sender, EventArgs e)
        {
           // PLAN_HocKyDangKy_TCCtrl.LoadDataSource(cmbNamHoc);
            gridView1.Config(true, false, false, false, false, false , false, false, false);
            gridView1.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            gridView1.GroupColumns(new[] { colTen_mon }, true);
          viewThongTinLopTinChi1BindingSource.DataSource = (new Context()).View_ThongTinLopTinChi_1s.ToList();
            gridView2.Config(true, false, false, false, false, false, false, false, false);
            gridView2.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
           gridView2.GroupColumns(new[] { colTen_he }, true);
           viewThongTinDangKy1BindingSource.DataSource = (new Context()).View_ThongTinDangKy_1s.ToList();
        //   repositoryItemButtonEdit1.Click += repositoryItemButtonEdit1_Click;
           
            

           PLAN_HocKyDangKy_TCCtrl.LoadDataSource(cmbNamHoc);
           gridView1.OptionsBehavior.Editable = true;
         //   gridView1.BestFitAllColumns();
        }

        private void cmbNamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            PLAN_HocKyDangKy_TCCtrl.LoadDataSource(cmbHocKy, cmbNamHoc.Text);
        }

        private void cmbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            PLAN_HocKyDangKy_TCCtrl.LoadDataSource(cmbDot, cmbNamHoc.Text, cmbHocKy.Text);
        }

      
        PLAN_HocKyDangKy_TC HocKyDK { get; set; }
        List<View_ThongTinLopTinChi_1> Thongtinloptinchis { get; set; }
        List<View_ThongTinDangKy_1> Sinhviendangkis { get; set; }

        private void chkKyPhu_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void cmbDot_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDot.Text == "1" && cmbHocKy.Text == "1A")
                chkKyPhu.Checked = true;
            else
                chkKyPhu.Checked = false;
       
            Context db = new Context();
            if (Thongtinloptinchis==null)
            Thongtinloptinchis = db.View_ThongTinLopTinChi_1s.ToList();
            var result = Thongtinloptinchis.Where(w => w.Nam_hoc == cmbNamHoc.Text && w.Hoc_ky == (cmbHocKy.Text=="1"?1:2) && w.Dot == int.Parse(cmbDot.Text));
            gridControl1.BeginUpdate();
            gridControl1.DataSource = result.ToList();
            gridControl1.RefreshDataSource();
            gridControl1.EndUpdate();
            if (Sinhviendangkis == null)
            Sinhviendangkis = db.View_ThongTinDangKy_1s.ToList();
            var result1 = Sinhviendangkis.Where(w => w.Nam_hoc == cmbNamHoc.Text && w.Hoc_ky == (cmbHocKy.Text == "1" ? 1 : 2) && w.Dot == int.Parse(cmbDot.Text));
            gridControl2.BeginUpdate();
            gridControl2.DataSource = result1.ToList();
            gridControl2.RefreshDataSource();
            gridControl2.EndUpdate();

            lblTongSoHocPhanDuocMo.Text = result.Select(s => s.Ten_mon).ToList().Count().ToString();
            lblTongSoLopHocPhanDuocMo.Text = result.Select(s => s.Ma_lop_tc).ToList().Count().ToString();
            lblSoLopLyThuyet.Text = result.Where(a => a.Kieu_hoc == "Lý thuyết").Select(s => s.Ma_lop_tc).ToList().Count().ToString();
            lblSoLopThucHanh.Text = result.Where(a => a.Kieu_hoc == "Thực hành").Select(s => s.Ma_lop_tc).ToList().Count().ToString();
           // this.BeginInvoke(new MethodInvoker(delegate()
           // {
            //    Context db = new Context();
            //    HocKyDK = PLAN_HocKyDangKy_TCCtrl.GetItem(cmbNamHoc.Text, cmbHocKy.Text, cmbDot.Text, db);
          
           // }));
        }

        private void gridControl2_Click(object sender, EventArgs e)
        {

        }

        private void cmbDot_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
           

        }

        protected void repositoryItemButtonEdit1_Click(object sender, EventArgs e)
        {
           // MaHocPhan = gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"colMa_lop_tc").ToString();
            //MaHocPhan = row[3].ToString();
            //string abc = gridView1.GetFocusedRowCellValue("colMa_lop_tc").ToString();
            //
            var result = gridView1.GetFocusedRow() as View_ThongTinLopTinChi_1;
            MaHocPhan = result.Ma_lop_tc;
            frmDanhSachLopTinChiChiTiet a = new frmDanhSachLopTinChiChiTiet();
            a.ShowDialog();
        }
    }
}
